﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Prism.ViewModel;
using Microsoft.Practices.Prism.Commands;
using System.Windows;
using System.ComponentModel;
using WebServerConnectorDll;

namespace FruitManagerApp
{
    public class MainWindowViewModel : NotificationObject
    {
        MainWindow gMainWindow = null;

        public MainWindowViewModel(Window pWindow)
        {
            gMainWindow = (MainWindow)pWindow;

            //Debug
            Username = "eeeeeee518";
            Password = "19b6518+";
        }

        private String errorMessage;
        public String ErrorMessage
        {
            get
            {
                return errorMessage;
            }

            set
            {
                errorMessage = value;
                RaisePropertyChanged("ErrorMessage");
            }
        }

        private String username;
        public String Username
        {
            get
            {
                return username;
            }

            set
            {
                username = value;
                RaisePropertyChanged("Username");
            }
        }

        private String password;
        public String Password
        {
            get
            {
                return password;
            }

            set
            {
                password = value;
                RaisePropertyChanged("Password");
            }
        }

        protected DelegateCommand<object> loginCommand;
        public DelegateCommand<object> LoginCommand
        {
            get
            {
                return loginCommand ?? (loginCommand = new DelegateCommand<object>(ExecuteLoginCommand));
            }
        }

        private void ExecuteLoginCommand(object obj)
        {
            HttpResponse response = API.Login(Username, Password);

            if(response.S == HttpResponse.Success)
            {
                LoginSuccess(response.D);
            }
            else
            {
                ErrorMessage = response.D;
            }
        }

        protected DelegateCommand<object> quitCommand;
        public DelegateCommand<object> QuitCommand
        {
            get
            {
                return quitCommand ?? (quitCommand = new DelegateCommand<object>(ExecuteQuitCommand));
            }
        }

        private void ExecuteQuitCommand(object obj)
        {
            if (MessageBoxResult.Yes == MessageBox.Show("您确定要退出吗?", "退 出", MessageBoxButton.YesNo))
            {
                gMainWindow.Close();
            }
        }

        private void LoginSuccess(String data)
        {
            List<CPersonnel> loginList = JSON.parse<List<CPersonnel>>(data);
            App.LoginInfo = loginList[0];

            ParentWindow parentWindow = new ParentWindow();
            ParentWindowViewModel mainWindowViewModel = new ParentWindowViewModel(parentWindow);
            parentWindow.DataContext = mainWindowViewModel;
            parentWindow.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner;
            parentWindow.Show();
            gMainWindow.Close();
        }
    }
}
